package com.etogc.groupchat.find;

/**
 * Created by McGrady on 2017/6/11.
 */

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etogc.groupchat.R;
import com.etogc.groupchat.common.WebActivity;
import com.etogc.util.GcUrl;
import com.etogc.util.ImageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 来访记录适配器
 */
public class FindAdapter extends RecyclerView.Adapter<FindAdapter.MyViewHolder> {

    private final Context mContext;

    private List<HashMap<String, String>> mList = new ArrayList<>();

    public FindAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<HashMap<String, String>> list) {
        if (list != null) {
            this.mList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_find, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Map<String, String> map = mList.get(position);
        String photo = String.valueOf(map.get("photo"));
        final String remark = String.valueOf(map.get("remark"));
        final String url = String.valueOf(map.get("url"));
        holder.tvRemark.setText(remark);
        if (photo != null && !TextUtils.isEmpty(photo)) {
            ImageUtils.setImage(mContext, ImageUtils.getImageUrl(photo), holder.picture);
        }
        holder.picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (url != null) {
                    Intent intent = new Intent(mContext, WebActivity.class);
                    intent.putExtra("url", url);
                    intent.putExtra("title", remark);
                    mContext.startActivity(intent);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView picture;
        TextView tvRemark;

        public MyViewHolder(View itemView) {
            super(itemView);
            picture = itemView.findViewById(R.id.picture);
            tvRemark = itemView.findViewById(R.id.tvRemark);
        }
    }
}